<template>
  <el-drawer v-model="drawerVisible" :destroy-on-close="true" size="25%" :title="`${drawerProps.title}-字典管理`">
    <el-form ref="ruleFormRef" label-width="100px" label-suffix=":" :rules="rules" :model="operateDataInfo">
      <el-space fill>
        <el-alert type="info" show-icon :closable="false">
          <p>字典项在字典项页中设置!</p>
        </el-alert>
        <el-form-item label="字典名称" prop="dictName">
          <el-input v-model="operateDataInfo.dictName" placeholder="请输入字典名称" clearable></el-input>
        </el-form-item>
        <el-form-item label="字典类型" prop="dictType">
          <el-input v-model="operateDataInfo.dictType" placeholder="请输入字典类型" clearable></el-input>
        </el-form-item>
        <el-form-item label="字典状态" prop="dictStatus">
          <el-select v-model="operateDataInfo.dictStatus" placeholder="请选择字典状态">
            <el-option
              v-for="item in drawerProps.enumMap?.get('dictStatus')"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="字典描述" prop="remark">
          <el-input v-model="operateDataInfo.remark" type="textarea" placeholder="请输入字典描述" clearable></el-input>
        </el-form-item>
      </el-space>
    </el-form>
    <template #footer>
      <el-button @click="drawerVisible = false">取消</el-button>
      <el-button type="primary" @click="handleSubmit">确定</el-button>
    </template>
  </el-drawer>
</template>

<script setup lang="ts">
import { reactive, ref } from "vue";
import { ElMessage, FormInstance } from "element-plus";
import { DictInfoResult, DictSaveParam, DictUpdateParam, getDictInfo } from "@/api/modules/sys/dict";

interface DrawerProps {
  title: string;
  isSave: boolean;
  isUpdate: boolean;
  row: Partial<DictInfoResult>;
  api?: (params: any) => Promise<any>;
  getTableList?: () => void;
  enumMap?: Map<string, { [key: string]: any }[]>;
}

const drawerVisible = ref(false);
const drawerProps = ref<DrawerProps>({
  title: "",
  isSave: false,
  isUpdate: false,
  row: {}
});

const rules = reactive({
  dictName: [{ required: true, message: "请填写字典名称" }],
  dictType: [{ required: true, message: "请填写字典类型" }]
});

const operateDataInfo = ref<DictSaveParam | DictUpdateParam>({ dictStatus: 1 });
const openDrawer = async (params: DrawerProps) => {
  if (params.isUpdate) {
    // 更新信息，先查询
    let { data } = await getDictInfo(params.row.id ? params.row.id : "");
    operateDataInfo.value = { ...data };
  } else if (params.isSave) {
    // 新增前的默认值
    operateDataInfo.value = { dictStatus: 1 };
  }
  drawerProps.value = params;
  drawerVisible.value = true;
};

// 提交数据（新增/编辑）
const ruleFormRef = ref<FormInstance>();
const handleSubmit = () => {
  ruleFormRef.value!.validate(async valid => {
    if (!valid) return;
    try {
      await drawerProps.value.api!(operateDataInfo.value);
      ElMessage.success({ message: `${drawerProps.value.title} 成功！` });
      drawerProps.value.getTableList!();
      drawerVisible.value = false;
    } catch (error) {
      console.log(error);
    }
  });
};

defineExpose({
  openDrawer
});
</script>

<style scoped lang="scss"></style>
